package com.zx.sp_mybatisplus_smbms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.sp_mybatisplus_smbms.pojo.SmbmsProvider;
import com.zx.sp_mybatisplus_smbms.pojo.SmbmsUser;
import com.zx.sp_mybatisplus_smbms.service.SmbmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private SmbmsUserService usersService;

    /**
     * @param userCode 前端页面的账户  name="userCode"
     * @param upwd     前端页面的密码   name="upwd"
     * @param session
     * @return
     */
    @RequestMapping("login")
    public String login(String userCode, String upwd, HttpSession session) {
        SmbmsUser user = usersService.login(userCode, upwd);
        if (user != null) {//成功
            session.removeAttribute("msg");
            session.setAttribute("logUser", user);
            // forward: 转发跳转到控制器
            return "forward:list";
        } else { //失败
            session.setAttribute("msg", "用户名或密码错误！");
            return "login";
        }

    }

    //查询所有
    @RequestMapping("list")
    public String list(Integer current, String userName, Long userRole, Model model) {

//        Page<SmbmsUser> page = new Page<>();
//        List<SmbmsUser> ulist = usersService.findAllByUsers();
//        QueryWrapper<SmbmsUser> wrapper = new QueryWrapper<>();
//        wrapper.like("userName",userName).eq("userRole",userRole);
//        List<SmbmsUser> ulist = usersService.list(wrapper);

//        List<SmbmsUser> ulist = usersService.findUserListByCodeAndRole(userName,userRole);

//if (null==current){
//    current=1;
//}
//        Page<SmbmsUser> page=new Page<SmbmsUser>(current,5);
//        QueryWrapper<SmbmsUser> wrapper =new QueryWrapper<>();
//        if(userName!=null&&userName!="null"){
//            wrapper.like("userName",userName);
//        }
//        if(userRole!=null&&!"null".equals(userRole)){
//            wrapper.eq("userRole",userRole);
//        }
//        Page<SmbmsUser> page1 = usersService.page(page, wrapper);
        if (null == current) {
            current = 1;
        }
        PageHelper.startPage(current, 4);
        List<SmbmsUser> ulist = usersService.findUserListByCodeAndRole(userName, userRole);
        PageInfo<SmbmsUser> page1 = new PageInfo<>(ulist);
        model.addAttribute("ulist", page1.getList());
        model.addAttribute("page1", page1);
        model.addAttribute("userRole", userRole);
        model.addAttribute("userName", userName);
        return "userlist";
    }


    //修改前查询
    @RequestMapping("goupdate")
    public String goupdate(Long uid, Model model) {
        SmbmsUser smbmsUser = usersService.findUsersById(uid);
        model.addAttribute("euser", smbmsUser);
        return "usermodify";
    }

    //修改操作
    @RequestMapping("doupdate")
    public String doupdate(SmbmsUser user, Model model) {
        boolean flag = usersService.updateUsers(user);
        if (flag) { //修改成功  再次查詢下
            return "forward:list";
        } else {  //修改失败
            model.addAttribute("msg", "修改失败！");
            return "usermodify";
        }

    }

    //详情页面
    @RequestMapping("details")
    public String details(Long uid, Model model) {
        SmbmsUser smbmsUser = usersService.findUsersById(uid);
        model.addAttribute("duser", smbmsUser);
        return "userview";
    }

    //删除操作
    @RequestMapping("del")
    public String del(Long uid, Model model) {
        boolean flag = usersService.delUsers(uid);
        if (flag) {
            return "redirect:list";
        } else {
            model.addAttribute("msg", " 删除失败");
            return "userlist";
        }
    }

    //添加操作
    @RequestMapping("goAdd")
    public String goadd() {
        return "useradd";
    }

    //添加操作
    @RequestMapping("add")
    public String add(SmbmsUser smbmsUser, Model model) {
        boolean flag = usersService.addUsers(smbmsUser);
        if (flag) {
            return "redirect:list";
        } else {
            model.addAttribute("msg", " 添加失败");
            return "useradd";
        }

    }

    //查询用户编码是否存在
    @RequestMapping("findCode")
    public @ResponseBody
    String findCode(@RequestParam("userCode") String userCode, HttpServletResponse response) throws Exception {
        SmbmsUser user = usersService.findUserByCode(userCode);
        if (user != null) {
            return "true";
        } else {
            return null;
        }
//        String josonResult = JSON.toJSONString(user);
//        ResponseUtil.write(response, josonResult);
//            return josonResult;
    }

//    //查询用户编码是否存在
//    @RequestMapping("findRole")
//    @ResponseBody
//    public List<SmbmsRole> findCode(HttpServletResponse response) throws Exception {
//        List<SmbmsRole> roleList = roleService.findAllRole();
////        String josonResult = JSON.toJSONString(roleList);
////        ResponseUtil.write(response, josonResult);
//        return roleList;
//    }

}
